package com.dstz.demo.core.manager.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dstz.base.api.response.impl.ResultMsg;
import com.dstz.base.rest.util.HttpClientUtil;
import com.dstz.demo.core.dao.*;
import com.dstz.demo.core.manager.ImportDataService;
import com.dstz.demo.core.model.Businfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;

@Service
public class ImportDataServiceImpl implements ImportDataService {

    @Resource
    private LineinfoDao lineinfoDao;
    @Resource
    private StationinfoDao stationinfoDao;
    @Resource
    private StationlineinfoDao stationlineinfoDao;
    @Resource
    private BusinfoDao businfoDao;
    @Resource
    private BusinfobylinenoDao businfobylinenoDao;

    @Override
    public ResultMsg httpRequest(String apiPath) {

        //调用的api的接口地址
        String apiUrl = apiPath;
        try {
//            URL url = new URL(apiPath);
//            //打开url之间的连接
//            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//            connection.setRequestProperty("Charset", "utf-8");
//            connection.connect();
//            result = new StringBuffer();
//            //读取URL的响应
//            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//            String line;
//            //每读取一行,判断如果有数据就赋值给result,直到获取所有数据
//            while ((line = reader.readLine()) != null) {
//                result.append(line);
//            }
            String respose = HttpClientUtil.httpGet(apiUrl, null, "UTF-8", 60 * 1000);
            //把respose转换为JSONArray
            JSONArray resultArray = JSONArray.parseArray(respose);
            for (int i = 0; i < resultArray.size(); i++) {
                JSONObject jsonObject = (JSONObject) resultArray.get(i);
                //定义一个接收类XXX，将接收的数据写进数据库
//              Stationinfo stationinfo = new Stationinfo();
//              stationinfo.setStationId(jsonObject.getString("stationId"));
//              stationinfo.setStationName(jsonObject.getString("stationName"));
//              stationinfo.setLng(jsonObject.getBigDecimal("lng"));
//              stationinfo.setLat(jsonObject.getBigDecimal("lat"));
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Businfo businfo = new Businfo();
                businfo.setBusNo(jsonObject.getString("busNo"));
                businfo.setLineNo(jsonObject.getString("lineNo"));
                businfo.setLng(jsonObject.getBigDecimal("lng"));
                businfo.setLat(jsonObject.getBigDecimal("lat"));
                businfo.setVelocity(jsonObject.getBigDecimal("velocity"));
                businfo.setOrientation(jsonObject.getString("orientation"));
                businfo.setIsUpDown(jsonObject.getInteger("isUpDown"));
                if (jsonObject.getString("siteTime") !=null){
                    businfo.setSiteTime(sdf.parse(jsonObject.getString("siteTime")));
                } else {
                    businfo.setSiteTime(null);
                }
                businfo.setRunState(jsonObject.getInteger("runState"));
                businfo.setIsStation(jsonObject.getInteger("isStation"));
                //保存到数据库
                businfoDao.create(businfo);
            }

            return ResultMsg.SUCCESS("操作成功");

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }
}
